/**
 * 
 */
package com.gr.staffpm.security.service;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.gr.staffpm.datatypes.Role;
import com.gr.staffpm.security.role.DetachableRoleModel;


/**
 * @author Graham Rhodes 2 May 2011 20:31:19
 */
public class SortableRoleDataProvider extends SortableDataProvider<Role> {

    private final UserService userService;
    private static final long serialVersionUID = 1L;

    public SortableRoleDataProvider(UserService userService) {
        //Set default sort
        setSort(Role.ROLE, true);
        this.userService = userService;
    }

    /**
     * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
     */
    @Override
    public Iterator<? extends Role> iterator(int first, int count) {
        SortParam sp = getSort();
        return userService.getAllRoles(first, count, sp.getProperty(), sp.isAscending()).iterator();
    }

    @Override
    public IModel<Role> model(Role role) {
        return new DetachableRoleModel(userService, role);
    }

    @Override
    public int size() {
        return userService.getAllRoles().size();
    }

}
